package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;



    @Override
    public SetmealVO getById(Long id) {

        //获取套餐基本信息
        Setmeal setmeal=setmealMapper.getById(id);
        String categoryName = categoryMapper.getCategoryNameById(id);
        SetmealVO setmealVO=new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setCategoryName(categoryName);


        //获取套餐和菜品的关联关系

        List<SetmealDish> list = setmealDishMapper.list(setmeal.getId());

        setmealVO.setSetmealDishes(list);

        return setmealVO;
    }

    @Override
    @Transactional
    public void insert(SetmealDTO setmealDTO) {


        //向套餐表中插入一条数据
        Setmeal setmeal=new Setmeal();

        BeanUtils.copyProperties(setmealDTO,setmeal);

        setmealMapper.insert(setmeal);

        List<SetmealDish> dishList = setmealDTO.getSetmealDishes();

        dishList.forEach((setmealDish) ->setmealDish.setSetmealId(setmeal.getId()));


        if(dishList!=null&&!dishList.isEmpty()){
            //向setmealDish中插入多条与之对应的记录

            setmealDishMapper.insertBatch(dishList);

        }



    }

    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {

        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());

        Page<SetmealVO> p = setmealMapper.listWithCategory(setmealPageQueryDTO.getCategoryId(),setmealPageQueryDTO.getName()
        ,setmealPageQueryDTO.getStatus());

        return new PageResult(p.getTotal(),p.getResult());
    }

    @Override
    @Transactional
    public void deleteByIds(List<Long> ids) {

        //删除套餐
        setmealMapper.deleteByIds(ids);


        //删除套餐内的菜品

        setmealDishMapper.deleteBySetmealIds(ids);


    }

    @Override
    public void update(SetmealDTO setmealDTO) {

        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);

        //更新套餐
        setmealMapper.update(setmeal);


        //更新套餐关联的菜品
        ArrayList<Long> ids = new ArrayList<>();
        ids.add(setmealDTO.getId());
        //先删除再插入
        setmealDishMapper.deleteBySetmealIds(ids);

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealDTO.getId()));
        setmealDishMapper.insertBatch(setmealDishes);
    }

    @Override
    public void modifyStatus(Integer status, Long id) {

        Setmeal setmeal=Setmeal.builder().status(status).id(id).build();

        //setmealMapper.modifyStatus(status,id);
        setmealMapper.update(setmeal);
    }

    @Override
    public  List<Setmeal>  getByCategoryId(Long categoryId) {


        return setmealMapper.getByCategoryId(categoryId);
    }

    @Override
    public List<Setmeal> getByCategoryIdEnabled(Long categoryId) {
        return setmealMapper.getByCategoryId(categoryId, StatusConstant.ENABLE);
    }

    @Override
    public List<DishItemVO> getDishsById(Long id) {

        List<SetmealDish> list = setmealDishMapper.getBySetmealId(id);

        List<DishItemVO> dishItemVOList=new ArrayList<>();

        list.forEach(setmealDish -> {
            DishItemVO dishItemVO = new DishItemVO();
            BeanUtils.copyProperties(setmealDish,dishItemVO);
            dishItemVOList.add(dishItemVO);
        });
        return dishItemVOList;
    }


}
